﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wh40klbWeb.Models;
using wh40klbModel;

namespace wh40klbWeb.Controllers
{
    public class VehiculeController : Controller
    {
        private WarhammerContext context = new WarhammerContext();

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Details(int id)
        {


            var vehiculesAvailibles = context.Vehicules
                .Where(v => v.availible && v.Army.availible);


            VehiculeViewModel vvm = vehiculesAvailibles.Where(v=>! (v is Transporter))
                .Select(v => new VehiculeViewModel
                {
                    ArmorFront = v.ArmorFront,
                    ArmorRear = v.ArmorRear,
                    ArmorSide = v.ArmorSide,
                    Army = new ArmyViewModel { 
                        Id=v.Army.Id,
                        Name=v.Army.Name
                    },
                    availible=v.availible,
                    BallisticSkill=v.BallisticSkill,
                    description=v.description,
                    Id=v.Id,
                    Name=v.Name,
                    SquadType=v.SquadType.Name,
                    SquadTypeSelected=v.SquadTypeId
                })
                .FirstOrDefault();
            if (vvm == null) return RedirectToAction("Index", "Army");
            return View(vvm);
        }

        public ActionResult Create(int id)
        {
            return View();
        } 
        [HttpPost]
        public ActionResult Create(int id, VehiculeViewModel collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Edit(int id)
        {
            return View();
        }
        [HttpPost]
        public ActionResult Edit(int id, VehiculeViewModel collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

       
        public ActionResult Delete(int id)
        {
            return View();
        }
        [HttpPost]
        public ActionResult Delete(int id, VehiculeViewModel collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
